package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.app.api.domain.AppUser;
import com.ruoyi.app.domain.AppShare;
import com.ruoyi.app.domain.bo.AppUserBo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.mapper.AppShareMapper;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.core.enums.UserStatus;
import com.ruoyi.common.core.exception.user.UserException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 用户管理Service业务层处理
 *
 * @author Zach
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
public class AppUserServiceImpl implements IAppUserService {

    private final AppUserMapper baseMapper;

    private final AppShareMapper appShareMapper;

    /**
     * 查询用户管理
     */
    @Override
    public AppUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户管理列表
     */
    @Override
    public TableDataInfo<AppUserVo> queryPageList(AppUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        Page<AppUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户管理列表
     */
    @Override
    public List<AppUserVo> queryList(AppUserBo bo) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUser> buildQueryWrapper(AppUserBo bo) {
        LambdaQueryWrapper<AppUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), AppUser::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserPassword()), AppUser::getUserPassword, bo.getUserPassword());
        lqw.eq(bo.getUserType() != null, AppUser::getUserType, bo.getUserType());
        lqw.eq(bo.getUserLevel() != null, AppUser::getUserLevel, bo.getUserLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getUserIdCard()), AppUser::getUserIdCard, bo.getUserIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getUserIdCardPath()), AppUser::getUserIdCardPath, bo.getUserIdCardPath());
        lqw.eq(StringUtils.isNotBlank(bo.getUserPath()), AppUser::getUserPath, bo.getUserPath());
        lqw.like(StringUtils.isNotBlank(bo.getUserIdName()), AppUser::getUserIdName, bo.getUserIdName());
        lqw.eq(bo.getUserRealStatus() != null, AppUser::getUserRealStatus, bo.getUserRealStatus());
        lqw.eq(bo.getSuperUserId() != null, AppUser::getSuperUserId, bo.getSuperUserId());
        lqw.eq(bo.getCertificationStatus() != null, AppUser::getCertificationStatus, bo.getCertificationStatus());
        lqw.eq(bo.getStatus() != null, AppUser::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增用户管理
     */
    @Override
    public Boolean insertByBo(AppUserBo bo) {
        AppUser add = BeanUtil.toBean(bo, AppUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户管理
     */
    @Override
    public Boolean updateByBo(AppUserBo bo) {
        AppUser update = BeanUtil.toBean(bo, AppUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    /**
     *  根据手机号查询用户
     *
     * @author Zach

 * @param phones
     * @return com.ruoyi.app.domain.AppUser
     * @date 2023/11/16 15:19
     */

    @Override
    public AppUser queryByPhone(String phones) {
        return baseMapper.selectOne(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getPhoneNumber, phones));
    }

    @Override
    @Transactional
    public void saveUser(AppUser appUserBo) {
        baseMapper.insert(appUserBo);
        long userId=appUserBo.getId();
        AppShare appShare=new AppShare();
        appShare.setUserId(userId);
        appShare.setCode(StringUtils.genRandomNum());
        appShare.setStatus(1L);
        appShareMapper.insert(appShare);
    }
}
